package com.baymax.security.permission;

import com.baymax.entity.vo.SysRoleVO;
import com.baymax.security.entity.SecurityUser;
import com.baymax.utils.SecurityUtil;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 接口权限校验
 *
 * @Author: baymax
 * @CreateTime: 2023/3/29 15:55
 * @Version: 1.0
 */
@Service(value = "el")
public class ElPermissionConfig {

    /**
     * 权限验证
     *
     * @param permissions 定义在接口上面的权限
     * @return true：验证通过 false：验证失败
     */
    public Boolean check(String ...permissions){
        // 获取用户在Security中的上下文信息
        SecurityUser currentUserInfo = SecurityUtil.getCurrentUserInfo();
        Set<String> roleTagList = currentUserInfo.getRoleVOList().stream().map(SysRoleVO::getRoleTag).collect(Collectors.toSet());
        // 判断当前用户的所有权限是否包含接口上定义的权限，有则验证通过
        return roleTagList.contains("administrator") || Arrays.stream(permissions).anyMatch(currentUserInfo.getMenuPermissionList()::contains);
    }

}

